package com.cssw.fyzb.core.mao.user;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.core.model.mos.UserMo;
import com.cssw.fyzb.core.mongo.MongoClient;
import com.cssw.fyzb.core.util.AesUtil;
import com.cssw.fyzb.core.util.DateUtil;
import com.cssw.fyzb.core.util.DocUtil;
import com.cssw.fyzb.core.util.StringUtil;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Feng Chen
 */
@Component
public class UserMaoImpl implements UserMao {

    @Autowired
    private MongoClient mongoClient;

    @Override
    public UserMo findByUserId(Long userId) {
        Document doc = mongoClient.getCollection("user")
                .find(new Document("userId", userId))
                .first();
        return DocUtil.decoce(doc, UserMo.class);
    }

    @Override
    public UserMo findByMobile(String mobile) {
        Document doc = mongoClient.getCollection("user")
                .find(new Document("mobile", mobile))
                .first();
        return DocUtil.decoce(doc, UserMo.class);
    }

    @Override
    public void save(Long userId, Long supUserId, String mobile, String loginPwd, String tradePwd, String nickName, String avatar) {
        UserMo userMo = new UserMo();
        userMo.setUserId(userId);
        userMo.setSupUserId(supUserId);
        userMo.setMobile(mobile);
        if (StringUtil.isNotEmpty(tradePwd)) {
            userMo.setTradePwd(AesUtil.encode(tradePwd));
        }
        if (StringUtil.isNotEmpty(loginPwd)) {
            userMo.setLoginPwd(AesUtil.encode(loginPwd));
        }
        userMo.setNickName(nickName);
        userMo.setAvatar(avatar);
        userMo.setAnchor("0");
        userMo.setFans(0);
        userMo.setFollows(0);
        MongoCollection collection = mongoClient.getCollection("user");

        Document doc = new Document(JSON.parseObject(JSON.toJSONString(userMo)));

        collection.insertOne(doc);
    }

    @Override
    public void saveUnion(Long userId, Long supUserId, String mobile, String nickName, String avatar, String loginPwd) {
        UserMo userMo = new UserMo();
        userMo.setUserId(userId);
        userMo.setSupUserId(supUserId);
        userMo.setMobile(mobile);
        userMo.setNickName(nickName);
        if (StringUtil.isNotEmpty(loginPwd)) {
            userMo.setLoginPwd(AesUtil.encode(loginPwd));
        }
        userMo.setAvatar(avatar);
        userMo.setFans(0);
        userMo.setFollows(0);

        MongoCollection collection = mongoClient.getCollection("user");

        Document doc = new Document(JSON.parseObject(JSON.toJSONString(userMo)));

        collection.insertOne(doc);
    }

    @Override
    public void saveQQ(Long userId, Long supUserId, String mobile, String userName, String nickName, String province, String city, String sex, String avatar) {
        UserMo userMo = new UserMo();
        userMo.setUserId(userId);
        userMo.setSupUserId(supUserId);
        userMo.setMobile(mobile);
        userMo.setUserName(userName);
        userMo.setNickName(nickName);
        userMo.setProvince(province);
        userMo.setCity(city);
        userMo.setSex(sex);
        userMo.setAvatar(avatar);
        userMo.setFans(0);
        userMo.setFollows(0);

        MongoCollection collection = mongoClient.getCollection("user");

        Document doc = new Document(JSON.parseObject(JSON.toJSONString(userMo)));

        collection.insertOne(doc);
    }

    @Override
    public void saveWechat(Long userId, Long supUserId, String mobile, String userName, String nickName, String province, String city, String sex, String avatar) {
        UserMo userMo = new UserMo();
        userMo.setUserId(userId);
        userMo.setSupUserId(supUserId);
        userMo.setMobile(mobile);
        userMo.setUserName(userName);
        userMo.setNickName(nickName);
        userMo.setProvince(province);
        userMo.setCity(city);
        userMo.setSex(sex);
        userMo.setAvatar(avatar);
        userMo.setFans(0);
        userMo.setFollows(0);

        MongoCollection collection = mongoClient.getCollection("user");

        Document doc = new Document(JSON.parseObject(JSON.toJSONString(userMo)));

        collection.insertOne(doc);
    }

    @Override
    public void updateImsi(Long userId, String imsi) {

        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("imsi", imsi);
        mongoClient.getCollection("user").updateOne(query,
                new Document("$set", update));
    }

    @Override
    public void updateLoginPwd(Long userId, String mobile, String loginPwd) {
        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("loginPwd", loginPwd);
        mongoClient.getCollection("user").updateOne(query,
                new Document("$set", update));
    }

    @Override
    public void updateTradePwd(Long userId, String mobile, String tradePwd) {
        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("tradePwd", tradePwd);
        mongoClient.getCollection("user").updateOne(query,
                new Document("$set", update));
    }

    @Override
    public void updateAvatar(Long userId, String avatar) {
        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("avatar", avatar);
        mongoClient.getCollection("user").updateOne(query,
                new Document("$set", update));
    }

    @Override
    public void updateNickName(Long userId, String nickName) {

        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("nickName", nickName);
        mongoClient.getCollection("user").updateOne(query,
                new Document("$set", update));
    }

    @Override
    public void updateSex(Long userId, String sex) {

        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("sex", sex);
        mongoClient.getCollection("user").updateOne(query,
                new Document("$set", update));
    }

    @Override
    public void updateBirthday(Long userId, String birthday) {

        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("birthday", DateUtil.parseMbl(birthday));
        mongoClient.getCollection("user").updateOne(query,
                new Document("$set", update));
    }

    @Override
    public void updateSign(Long userId, String sign) {

        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("sign", sign);
        mongoClient.getCollection("user").updateOne(query,
                new Document("$set", update));
    }

    @Override
    public void updateFaceAuth(Long userId, String status) {

        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("faceAuth", status);
        mongoClient.getCollection("user").updateOne(query,
                new Document("$set", update));
    }

    @Override
    public void updateAuthInfo(Long userId, String userName, String province, String city, String district, String idCard) {
        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("userName", userName);
        update.put("province", province);
        update.put("city", city);
        update.put("district", district);
        update.put("idCard", idCard);
        mongoClient.getCollection("user").updateOne(query,
                new Document("$set", update));
    }

    @Override
    public void updateAnchor(Long userId) {
        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("anchor", "1");
        mongoClient.getCollection("user").updateOne(query, new Document("$set", update));
    }

    @Override
    public void incLikes(Long userId) {
        UserMo userMo = findByUserId(userId);
        if (null == userMo ){
            return;
        }
        if (null == userMo.getLikes()){
            // 加 一 操作
            mongoClient.getCollection("user").updateOne(
                    new Document("userId", userId),
                    new Document("$set", new Document("likes", 1))
            );
        } else {
            // 加 一 操作
            mongoClient.getCollection("user").updateOne(
                    new Document("userId", userId),
                    new Document("$inc", new Document("likes", 1))
            );
        }
    }

    @Override
    public void decLikes(Long userId) {
        UserMo userMo = findByUserId(userId);
        if (null == userMo ){
            return;
        }

        if (null == userMo.getLikes() || userMo.getLikes() < 1){
            return;
        }

        // 减 一 操作
        mongoClient.getCollection("user").updateOne(
                new Document("userId", userId),
                new Document("$inc", new Document("likes", -1))
        );
    }


    @Override
    public void incFans(Long userId) {
        // 加 一 操作
        mongoClient.getCollection("user").updateOne(
                new Document("userId", userId),
                new Document("$inc", new Document("fans", 1))
        );
    }

    @Override
    public void decFans(Long userId) {
        // 减 一 操作
        mongoClient.getCollection("user").updateOne(
                new Document("userId", userId),
                new Document("$inc", new Document("fans", -1))
        );
    }

    @Override
    public void incFollows(Long userId) {
        // 加 一 操作
        mongoClient.getCollection("user").updateOne(
                new Document("userId", userId),
                new Document("$inc", new Document("follows", 1))
        );
    }

    @Override
    public void decFollows(Long userId) {
        // 减 一 操作
        mongoClient.getCollection("user").updateOne(
                new Document("userId", userId),
                new Document("$inc", new Document("follows", -1))
        );
    }

    @Override
    public void updateLive(Long userId, String live) {
        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("live", live);
        mongoClient.getCollection("user").updateOne(query, new Document("$set", update));
    }


    @Override
    public void updateStatus(Long userId, String status) {
        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("status", status);
        mongoClient.getCollection("user").updateOne(query, new Document("$set", update));
    }

    @Override
    public void update(UserMo userMo) {
        Document query = new Document();
        query.put("userId", userMo.getUserId());

        mongoClient.getCollection("user").updateOne(query, new Document("$set", DocUtil.encode(userMo)));
    }

    @Override
    public void save(UserMo userMo) {
        mongoClient.getCollection("user").insertOne(DocUtil.encode(userMo));
    }

    @Override
    public void updateVip(Long userId, String vip) {
        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("vip", vip);
        mongoClient.getCollection("user").updateOne(query, new Document("$set", update));
    }

    @Override
    public void updateLikes(Long userId, Integer likes) {
        Document query = new Document();
        query.put("userId", userId);

        Document update = new Document();
        update.put("likes", likes);
        mongoClient.getCollection("user").updateOne(query, new Document("$set", update));
    }
}
